package br.com.bbcalibracao.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import br.com.bbcalibracao.bean.Calibracao;
import br.com.bbcalibracao.bean.CalibracaoPdf;
import br.com.bbcalibracao.dao.CalibracaoDAO;
import br.com.bbcalibracao.form.CalibracaoForm;
import br.com.bbcalibracao.util.Constantes;
import br.com.bbcalibracao.util.Validador;

public class DetalhaCalibracaoAction extends org.apache.struts.action.Action {

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
	@Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        try {
	        HttpSession sessao = request.getSession();
	        if (Validador.validaSessao(sessao)){
	        	CalibracaoForm pdfForm = (CalibracaoForm) form;
	        	int id = pdfForm.getId();
	        	if (id==0){
	        		throw new NumberFormatException();
	        	}
	        	Calibracao cal = obtemCalibracao(id);
	        	List<CalibracaoPdf> listaCal = obtemCalibracoesPdf(id);
	            request.setAttribute("cal", cal);
	            request.setAttribute("listaCal", listaCal);
	            return mapping.findForward(Constantes.SUCCESS);
	        } else
	        	return mapping.findForward(Constantes.NAO_LOGADO);
        } catch (NumberFormatException e) {
        	return mapping.findForward(Constantes.LOGADO);
        } catch (Exception e) {
        	e.printStackTrace();
        	return mapping.findForward(Constantes.ERROR);
		}
        
    }
	
	public Calibracao obtemCalibracao(int id){
		CalibracaoDAO cDao = new CalibracaoDAO();
		Calibracao cal = cDao.obtemCalibracao(id);
		return cal;
    }
	public List<CalibracaoPdf> obtemCalibracoesPdf(int id){
		CalibracaoDAO cDao = new CalibracaoDAO();
		List<CalibracaoPdf> cal = cDao.obtemCalibracoesPdf(id);
		return cal;
    }
}
